package cn.csust.net2.manager.server.util;

import java.util.ArrayList;
import java.util.List;

import cn.csust.net2.manager.shared.po.Cell;
import cn.csust.net2.manager.shared.po.PairCell;
import cn.csust.net2.manager.shared.po.Table;
import cn.csust.net2.manager.shared.vo.TableVO;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class TableFactory {
	/**
	 * �����Height
	 */
	public static final String HEIGHT = "Height";
	/**
	 * �����Width
	 */
	public static final String WIDTH = "Width";
	/**
	 * �����Cell
	 */
	public static final String CELL = "Cell";
	/**
	 * �����Range
	 */
	public static final String RANGE = "Range";
	/**
	 * �����Text
	 */
	public static final String TEXT = "Text";
	/**
	 * �����Word.Application
	 */
	public static final String WORD_APPLICALTION = "Word.Application";
	/**
	 * �����Visible
	 */
	public static final String VISIVBLE = "Visible";
	/**
	 * �����Documents
	 */
	public static final String DOCUMENTS = "Documents";
	/**
	 * �����Open
	 */
	public static final String OPEN = "Open";
	/**
	 * �����Tables
	 */
	public static final String TABLES = "Tables";
	/**
	 * �����Item
	 */
	public static final String ITEM = "Item";
	/**
	 * �����
	 */
	public static final String ROWS = "Rows";
	/**
	 * �����Columns
	 */
	public static final String COLUMNS = "Columns";
	/**
	 * �����Count
	 */
	public static final String COUNT = "Count";
	/**
	 * �����Close
	 */
	public static final String CLOSE = "Close";
	/**
	 * �����Quit
	 */
	public static final String QUIT = "Quit";
	
	
	@SuppressWarnings("deprecation")
	private static Cell getCell1(Dispatch dTable, int nRow, int nCell) {
		try {
			Cell cell = null;
			Dispatch dCell = Dispatch.call(dTable, TableFactory.CELL, new Integer(nRow),new Integer(nCell)).toDispatch();
			if (dCell != null) {
				cell = new Cell();
				int height = Dispatch.get(dCell, TableFactory.HEIGHT).toInt();
				int  width= Dispatch.get(dCell, TableFactory.WIDTH).toInt();
				Dispatch range = Dispatch.get(dCell, TableFactory.RANGE).toDispatch();
				String content = Dispatch.get(range, TableFactory.TEXT).toString().trim();
				cell.setWidth(width);
				cell.setHeight(height);
				System.out.println("width:"+width+"height:"+height);
				cell.setContent(content);
			}

			return cell;
		} catch (ComFailException cfe) {
			/** ���кϲ��е���� */
			return null;
		}
	}

	@SuppressWarnings("deprecation")
	public static TableVO buildTable(String docUrl) {
		Dispatch wordDoc = null;
		ActiveXComponent word = null;
		try {
			word = new ActiveXComponent(TableFactory.WORD_APPLICALTION);
			word.setProperty(TableFactory.VISIVBLE, new Variant(false));
			Dispatch documents = word.getProperty(TableFactory.DOCUMENTS).toDispatch();
			wordDoc = Dispatch.call(documents, TableFactory.OPEN, docUrl).toDispatch();
			Dispatch tables = Dispatch.get(wordDoc, TableFactory.TABLES).toDispatch();
			Dispatch table = Dispatch.call(tables, TableFactory.ITEM, new Variant(1)).toDispatch();
			Dispatch rows = Dispatch.get(table, TableFactory.ROWS).toDispatch();
			Dispatch columns = Dispatch.get(table,TableFactory.COLUMNS).toDispatch();
			int rowCount = Dispatch.get(rows, TableFactory.COUNT).toInt();
			int colCount = Dispatch.get(columns, TableFactory.COUNT).toInt();

			List<PairCell> pairs = new ArrayList<PairCell>();
			PairCell pairCell = null;
			boolean isKey = true;
			for (int i = 1; i <= rowCount; i++) {
				for (int k = 1; k <= colCount; k++) {
					Cell cell = getCell1(table, i, k);
					if (cell != null && isKey) {
						
						pairCell = new PairCell();
						pairCell.setKey(cell);
						pairCell.setRow(i);

						isKey = false;

					} else if (pairCell != null && cell != null && !isKey) {
						
						pairCell.setValue(cell);
						pairs.add(pairCell);
						pairCell.setCol(k/2);
						pairCell = null;
						isKey = true;
					}
				}

			}
			System.out.println("表格一共"+colCount+"列");
//			Table t = new Table("��û��ȡ", "��û��ȡ", colCount/2, rowCount, pairs);
			Table t = new Table();
			t.setFooter("");
			t.setTitle("");
			t.setRowCount(rowCount);
			t.setColCount(colCount/2);
			TableVO tableVO = new TableVO();
			tableVO.setList(pairs);
			tableVO.setTable(t);
			
			return tableVO;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispatch.call(wordDoc,TableFactory.CLOSE, new Variant(true));
			word.invoke(TableFactory.QUIT, new Variant[0]);
		}
		return null;
	}
}
